<?php

declare (strict_types = 1);

namespace app\common\repository\user;

use app\common\repository\BaseRepository;
use app\common\model\user\UserTag;
use think\db\exception\DbException;

class UserTagRepository extends BaseRepository
{
    protected \app\common\model\BaseModel $model;

    public function __construct(UserTag $model)
    {
        $this->model = $model;
    }

    /**
     * 获取标签列表
     *
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     * @throws DbException
     */
    public function getTagList(array $where, int $page, int $limit): array
    {
        // 创建查询构造器实例
        $queryBuilder = $this->model->with(['userTagCate' => function ($query) {
            $query->field(['id', 'cate_name']);
        }]);

        // 添加分类ID筛选条件
        if (!empty($where['cate_id'])) {
            $queryBuilder->where('cate_id', $where['cate_id']);
        }

        // 执行分页查询
        $posts = $queryBuilder->order('create_time', 'desc') // 根据需要调整排序规则
        ->paginate([
            'page' => $page,
            'list_rows' => $limit,
        ]);

        // 返回分页数据
        return [
            'total' => $posts->total(), // 总记录数
            'list' => $posts->items(), // 当前页的数据
        ];
    }
}